<?php


namespace app\adminapi\validate\user;


use app\common\model\user\User;
use app\common\model\user\UserType;
use app\common\validate\BaseValidate;

/**
 * @projectName src
 */
class UserValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkUser',
        'user_name' => 'require',
        'user_gender'=>'require|in:1,2,3',
        'user_idno'=>'require|idCard|unique:'.User::class,
        'user_type'=>'require|egt:0|checkUserType',
        'user_phone'=>'require'
    ];

    protected $message = [
        'id.require' => '参数缺失',
        'user_name.require' => '请填写用户姓名',
        'user_gender.require'=>'请填写用户性别',
        'user_gender.in'=>'用户性别参数不合法',
        'user_idno.require'=>'请填写身份证号',
        'user_idno.idCard'=>'身份证参数不合法',
        'user_type.require'=>'请填写用户类型',
        'user_type.egt:0'=>'用户类型参数不合法',
        'user_phone.require'=>'请填写手机号码'
    ];

    /**
     * @notes 添加场景
     * sceneAdd
     */
    public function sceneAdd()
    {
        return $this->remove('id', true);
    }

    /**
     * @notes 详情场景
     * sceneAdd
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * @notes 编辑场景
     * sceneAdd
     */
    public function sceneEdit()
    {
    }

    /**
     * @notes 删除场景
     * sceneAdd
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    public function checkUser($value)
    {
        $user = User::findOrEmpty($value);
        if ($user->isEmpty()) {
            return '用户不存在';
        }
        return true;
    }

    public function checkUserType($value)
    {
        $userType = UserType::findOrEmpty($value);
        if ($userType->isEmpty()) {
            return '用户类型不存在';
        }
        return true;
    }
}